package com.jzo2o.foundations.xxljob;

import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

import static com.jzo2o.foundations.constants.RedisConstants.CacheName.*;

@Component
@Slf4j
public class JobHandler {
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    @Resource
    private IRegionService regionService;
    @Resource
    private IServeService serveService;

    @XxlJob("buildRegionCaChe")
    public void buildEnableReginCaChe() {
        // 重构开通区域缓存
        redisTemplate.delete(JZ_CACHE + "::ACTIVE_REGIONS");
        List<RegionSimpleResDTO> regionSimpleResDTOS =
                regionService.queryActiveRegionListCache();

        // 重构首页服务列表
        for (RegionSimpleResDTO region : regionSimpleResDTOS) {
            redisTemplate.delete(SERVE_ICON + "::" + region.getId());
            serveService.firstPageServeList(region.getId());
        }

        // 重构服务类型列表缓存
        for (RegionSimpleResDTO region : regionSimpleResDTOS) {
            redisTemplate.delete(SERVE_TYPE + "::" + region.getId());
            serveService.selectServeTypeList(region.getId());
        }

        // 重构首页热门服务数据缓存
        for (RegionSimpleResDTO region : regionSimpleResDTOS) {
            redisTemplate.delete(HOT_SERVE + "::" + region.getId());
            serveService.hotServeList(region.getId());
        }
    }
}
